/*
 * Copyright (c) Jack魏 2023 - 2023, All Rights Reserved.
 */

package cn.jackwei.yiyi;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类
 *
 * @author Jack魏
 * @since 2023/6/18
 */
@Slf4j
@SpringBootApplication
@MapperScan("cn.jackwei.yiyi.mapper")
public class YiYiWebApplication {
    /**
     * 主程序入口
     *
     * @param args 启动参数
     * @throws UnknownHostException ip异常
     */
    public static void main(String[] args) throws UnknownHostException {
        SpringApplication.run(YiYiWebApplication.class, args);
        info();
        checkFilesPath();
    }

    /**
     * 启动信息
     *
     * @throws UnknownHostException ip异常
     */
    public static void info() throws UnknownHostException {
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = System.getProperty("server.port");
        String path = System.getProperty("server.servlet.context-path");
        String url = "http://" + ip + ":" + port + path;
        String swaggerPrefix = path.endsWith("/") ? "" : "/";
        log.info("后台调试接口：{}doc.html", url + swaggerPrefix);
        log.info("后台启动链接：{}", url);
    }

    /**
     * 上传文件夹创建
     */
    public static void checkFilesPath() {
        String filePath = System.getProperty("yiyi.file.path");
        // 不存在就创建文件夹
        if (!FileUtil.exist(filePath)) {
            final File mkdir = FileUtil.mkdir(filePath);
            if (mkdir == null || !mkdir.isDirectory()) {
                log.error("项目启动需要创建上传文件夹，请检查：{} 配置是否正确！", filePath);
            }
        }
    }
}
